package com.adk.backend.service.impl;

import com.adk.backend.common.PageResult;
import com.adk.backend.entity.LxInventoryList;
import com.adk.backend.mapper.InventoryListMapper;
import com.adk.backend.service.WarehouseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 仓库管理服务实现类
 */
@Slf4j
@Service
public class WarehouseServiceImpl extends ServiceImpl<InventoryListMapper, LxInventoryList> implements WarehouseService {
    
    @Override
    public PageResult<Map<String, Object>> getInventoryList(Map<String, Object> params) {
        Integer pageNum = (Integer) params.getOrDefault("pageNum", 1);
        Integer pageSize = (Integer) params.getOrDefault("pageSize", 20);
        
        Long total = baseMapper.countInventoryListByCondition(params);
        List<Map<String, Object>> list = baseMapper.selectInventoryListByCondition(params);
        
        return PageResult.of(total, list, pageNum, pageSize);
    }
    
    @Override
    public List<LxInventoryList> getInventoryByWhId(Long whId) {
        return baseMapper.selectByWhId(whId);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public LxInventoryList inbound(LxInventoryList inventory) {
        inventory.setAddTime((int) (System.currentTimeMillis() / 1000));
        this.save(inventory);
        return inventory;
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean outbound(Long inventoryId, Integer qty) {
        LxInventoryList inventory = this.getById(inventoryId);
        if (inventory == null) {
            throw new RuntimeException("库存记录不存在");
        }
        
        if (inventory.getQty() < qty) {
            throw new RuntimeException("库存不足");
        }
        
        inventory.setQty(inventory.getQty() - qty);
        inventory.setUpdateTime((int) (System.currentTimeMillis() / 1000));
        
        return this.updateById(inventory);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean transfer(Long fromWhId, Long toWhId, List<Map<String, Object>> transferList) {
        if (fromWhId.equals(toWhId)) {
            throw new RuntimeException("源仓库和目标仓库不能相同");
        }
        
        for (Map<String, Object> item : transferList) {
            Long inventoryId = Long.valueOf(item.get("inventoryId").toString());
            Integer qty = Integer.valueOf(item.get("qty").toString());
            
            // 减少源仓库库存
            LxInventoryList fromInventory = this.getById(inventoryId);
            if (fromInventory == null || !fromInventory.getWhId().equals(fromWhId)) {
                throw new RuntimeException("库存记录不存在或不属于源仓库");
            }
            
            if (fromInventory.getQty() < qty) {
                throw new RuntimeException("库存不足");
            }
            
            fromInventory.setQty(fromInventory.getQty() - qty);
            this.updateById(fromInventory);
            
            // 增加目标仓库库存
            LxInventoryList toInventory = new LxInventoryList();
            toInventory.setOlId(fromInventory.getOlId());
            toInventory.setProId(fromInventory.getProId());
            toInventory.setWhId(toWhId);
            toInventory.setVndId(fromInventory.getVndId());
            toInventory.setLocationId(fromInventory.getLocationId());
            toInventory.setLocationCode(fromInventory.getLocationCode());
            toInventory.setLocationSz(fromInventory.getLocationSz());
            toInventory.setSlId(fromInventory.getSlId());
            toInventory.setOrderNo(fromInventory.getOrderNo());
            toInventory.setQty(qty);
            toInventory.setCtns(fromInventory.getCtns());
            toInventory.setCtnPcs(fromInventory.getCtnPcs());
            toInventory.setCurrentCtn(fromInventory.getCurrentCtn());
            toInventory.setBatchCode(fromInventory.getBatchCode());
            toInventory.setLdId(fromInventory.getLdId());
            toInventory.setAddUid(fromInventory.getAddUid());
            toInventory.setAddTime((int) (System.currentTimeMillis() / 1000));
            this.save(toInventory);
        }
        
        return true;
    }
}

